package com.egreen.terminal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egreen.common.Result;
import com.egreen.common.States;
import com.egreen.common.pojo.entity.terminal.Transactionrecords;
import com.egreen.common.serviceapi.asset.IAssetServiceApi;
import com.egreen.terminal.dao.TransactionrecordsDao;
import com.egreen.terminal.service.ITransactionrecordsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-07-16
 */
@Service
public class TransactionrecordsServiceImpl extends ServiceImpl<TransactionrecordsDao, Transactionrecords> implements ITransactionrecordsService {

    @Autowired
    TransactionrecordsDao transactionrecordsDao;
    @DubboReference
    IAssetServiceApi iAssetServiceApi;

    /**
     * 获取到各种参数后对数据库进行写入实现终端交易记录
     *
     * @param tradingUser
     * @param tradingMoney
     * @param tradingPlace
     * @param serialNumber
     * @param tradingComment
     * @return
     */
    @Override
    public Result recyclingAtTerminal(String tradingUser, String tradingMoney, String tradingPlace,
                                      String serialNumber, String tradingComment) {

        Transactionrecords transactionrecords = new Transactionrecords();
        BigDecimal tradMoney = new BigDecimal(tradingMoney);
        transactionrecords.setTradingUser(tradingUser).setTradingMoney(tradMoney).setTradingTime(LocalDateTime.now())
                .setTradingPlace(tradingPlace).setSerialNumber(serialNumber).setTradingComment(tradingComment);
        //对资产模块的用户余额表更改余额并产生一条余额交易记录
        iAssetServiceApi.userBalanceChange(tradingUser, tradingMoney);

        int insert = transactionrecordsDao.insert(transactionrecords);
        if (insert != 1) {
            return Result.Error(States.ERR_TERMINAL_TRANSACTIONRECORDS_INSERT);
        } else {
            return Result.OK();
        }

    }

    /**
     * 根据serialNumber终端序列号删除该终端产生的所有记录
     *
     * @param serialNumber
     * @return
     */
    @Override
    public Result deleteTerminalTransaction(String serialNumber) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.likeRight("serial_number", serialNumber);
        int deleteNum = transactionrecordsDao.delete(queryWrapper);
        if (deleteNum == 0) {
            Result.Error(States.ERR_TERMINALCOUNT_TERMINAL_DELETE);
        }
        return Result.OK();
    }

    @Override
    public List inquireTerminalTransaction(String serialNumber) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.likeRight("serial_number", serialNumber);
        List selectMaps = transactionrecordsDao.selectMaps(queryWrapper);
        return selectMaps;
    }
}
